package com.linkhub.oms.admin.modules.product.service.impl;

import com.linkhub.oms.admin.common.exception.ExceptionFactory;
import com.linkhub.oms.admin.modules.order.service.CustomerOrderBizService;
import com.linkhub.oms.admin.modules.product.domain.param.ShopProductMappingParam;
import com.linkhub.oms.admin.modules.product.service.ProductMappingBizService;
import com.linkhub.oms.common.util.CacheKeyAssist;
import com.linkhub.oms.component.redis.locker.DistributedLocker;
import com.linkhub.oms.dao.mpservice.IShopProductSkuMappingService;
import com.linkhub.oms.entity.ShopProductSkuMappingEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

import static com.linkhub.oms.common.constant.ShopifyConstant.PRODUCT_DATA_TYPE_KEY;

/**
 * @Description: ProductMappingBizService
 * @Author: echo
 * @Date: 2024/3/23 20:28
 * @Version: 1.0
 */
@Slf4j
@Service
public class ProductMappingBizServiceImpl implements ProductMappingBizService {
    @Resource
    private IShopProductSkuMappingService shopProductSkuMappingService;
    @Resource
    private DistributedLocker distributedLocker;
    @Resource
    private CustomerOrderBizService customerOrderBizService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveShopProductMapping(ShopProductMappingParam productMappingParam) {

        String sysShopId = productMappingParam.getSysShopId();
        String lockKey = CacheKeyAssist.getShopifyDataSyncLockKey(sysShopId, PRODUCT_DATA_TYPE_KEY);
        try {
            if (!distributedLocker.tryLock(lockKey)) {
                throw ExceptionFactory.getI18nBizException("shop.product.sync.concurrence.error");
            }

            shopProductSkuMappingService.save(convert(productMappingParam));
            customerOrderBizService.runPendingMatchOrders(sysShopId);
        } finally {
            distributedLocker.unlock(lockKey);
        }
    }

    @Override
    public void deleteMapping(ShopProductMappingParam productMappingParam) {
        shopProductSkuMappingService.deleteMapping(productMappingParam.getSysShopId(),
                productMappingParam.getShopSkuId());

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMapping(ShopProductMappingParam productMappingParam) {

        String sysShopId = productMappingParam.getSysShopId();
        String lockKey = CacheKeyAssist.getShopifyDataSyncLockKey(sysShopId, PRODUCT_DATA_TYPE_KEY);
        try {
            if (!distributedLocker.tryLock(lockKey)) {
                throw ExceptionFactory.getI18nBizException("shop.product.sync.concurrence.error");
            }

            this.deleteMapping(productMappingParam);
            this.saveShopProductMapping(productMappingParam);
        } finally {
            distributedLocker.unlock(lockKey);
        }


    }

    private ShopProductSkuMappingEntity convert(ShopProductMappingParam productMappingParam) {
        ShopProductSkuMappingEntity entity = new ShopProductSkuMappingEntity();
        entity.setSysShopId(productMappingParam.getSysShopId());
        entity.setShopSkuId(productMappingParam.getShopSkuId());
        entity.setSkuId(productMappingParam.getLocalSkuId());
        entity.setCreatedTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());

        return entity;

    }
}
